﻿// // -------------------------------------------------------------------------------------
// // Copyright 2012 ELEN7046 Group 2
// // 
// // Last Modified on: 2012-06-10
// // 
// // Contributors:
// // 
// // Amith Sewnarain
// // Elmar Vosloo
// // Jason Thorne
// // Satish Jewan
// // 
// // Wits University 
// // School of Electrical & Information Engineering
// // Software Engineering Tools and Techniques
// // 
// // -------------------------------------------------------------------------------------
// // 
// // This file is part of the DishDash Event Visualization System.
// // 
// // DishDash is free software: you can redistribute it and/or modify
// // it under the terms of the GNU General Public License as published by
// // the Free Software Foundation, either version 3 of the License, or
// // (at your option) any later version.
// // 
// // DishDash is distributed in the hope that it will be useful,
// // but WITHOUT ANY WARRANTY; without even the implied warranty of
// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// // GNU General Public License for more details.
// // 
// // You should have received a copy of the GNU General Public License
// // along with DishDash.  
// // If not, see <http://www.gnu.org/licenses/>.
// // -------------------------------------------------------------------------------------


/// <reference path="../services/EventFetchingClientSideService.js" />
/// <reference path="~/js/flotr2.min.js" />

var dishdash = dishdash || { };
dishdash.graphs = dishdash.graphs || { };

dishdash.graphs.eventDistributionByContextValueWidget = function(targetId, widgetConfig) {
    this.renderTarget = document.getElementById(targetId);
    this.configuration = widgetConfig;
    this.categoryService = new dishdash.services.EventFetchingClientSideService();

    var graph = null;
    var self = this;

    this.draw = function(data, metaData) {

        graph = window.Flotr.draw(this.renderTarget, data, {
            radar: { show: true },
            grid: { circular: true, minorHorizontalLines: true },
            yaxis: { min: metaData.minCount, max: metaData.maxCount, minorTickFreq: metaData.tickFreq },
            xaxis: { ticks: metaData.tickData },
            mouse: { track: true },
            legend: { position: 'se', backgroundColor: '#D2E8FF' },
            title: "Event Spread by Context"
        });
    };

    this.start = function() {


        //var startTime = new Date(Date.parse("2012/01/01 00:00:00", "yyyy/MM/dd HH:mm:ss"));

        var now = new Date();
        var startTime = new Date(now - (this.configuration.samplePeriod * 1000));

        var request = {
            StartTime: startTime,
            EventNames: this.configuration.events,
            ContextKey: this.configuration.ContextKey,
            ContextValue: this.configuration.ContextValue,
            Precision: this.configuration.precision
        };
        this.categoryService.AggregateEventCountByContextValue(request, this.serviceCallback);
    };

    this.serviceCallback = function(response) {

        var ticks = [];

        var dataPoints = {
            data: [],
            label: ""
        };

        var metaData = {
            tickData: 0,
            minCount: 0,
            maxCount: 0,
            tickFreq: 0
        };


        if (!response.Error && response !== undefined && response.EventCounts[0].EventCounts.length !== 0) {
            //added this line to get rid of count error on startup

            var maxValue = response.EventCounts[0].EventCounts[0].Count;
            var minValue = response.EventCounts[0].EventCounts[0].Count;

            for (var i = 0; i < response.EventCounts.length; i++) {
                dataPoints.data.push([i, response.EventCounts[i].EventCounts[0].Count]);
                if (i === (response.EventCounts.length - 1)) {
                    dataPoints.label = self.configuration.ContextKey + "=" + self.configuration.ContextValue;
                }

                if (response.EventCounts[i].EventCounts[0].Count > maxValue) {
                    maxValue = response.EventCounts[i].EventCounts[0].Count;
                } else if (response.EventCounts[i].EventCounts[0].Count < minValue) {
                    minValue = response.EventCounts[i].EventCounts[0].Count;
                }

                ticks.push([i, response.EventCounts[i].EventName]);
            }

            metaData = {
                tickData: ticks,
                minCount: minValue,
                maxCount: maxValue,
                tickFreq: 1
            }; //added this to get rid of count error
        } else {
            dataPoints = {
                data: [0, 0],
                label: ""
            };
            metaData.title = "No Data Available";
        }

        self.draw(dataPoints, metaData);

        setTimeout(function() {
            self.start();
        }, self.configuration.refreshPeriod * 1000);
    };

};